package com.hb.trade.api.routine.controller.center.au;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.center.au.dto.OrgFileRepositoryDTO;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.api.routine.entity.ApiUploadEntity;
import com.hb.trade.client.center.OrgFileRepositoryClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.file.base.FileBaseController;
import org.fzzn.core.file.entity.HbFileInfo;
import org.fzzn.core.file.entity.UploadEntity;
import org.fzzn.core.file.service.PersistenceService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;


/**
 * describe:
 * 部门信息管理
 *
 * @author zhongdl
 * @date 2022/01/20
 */
@Slf4j
@Version(1.0)
@Api(tags = {"企业资料库"})
@RequestMapping("/{version}")
@RestController
public class ApiOrgFileRepositoryController extends FileBaseController {

    protected final OrgFileRepositoryClient orgFileRepositoryClient;

    public ApiOrgFileRepositoryController(PersistenceService service,
                                          OrgFileRepositoryClient orgFileRepositoryClient) {
        super(service);
        this.orgFileRepositoryClient = orgFileRepositoryClient;
    }

    @ApiOperation(value = "分页查询企业资料列表", notes = "分页查询企业资料列表")
    @PostMapping("/orgFileRepository/pageFile")
    public ResponseDto pageFile(@RequestBody OrgFileRepositoryDTO orgFileRepositoryDTO) {
        orgFileRepositoryDTO.setCreateId(ShiroUtil.getUserId());

        Message<PageResult<OrgFileRepositoryDTO>> pageResultMessage = orgFileRepositoryClient.pageOrgFile(orgFileRepositoryDTO);
        return ResultUtil.msg(pageResultMessage);
    }

    @ApiOperation(value = "删除文档", notes = "删除文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文档id", dataTypeClass = Long.class, required = true)
    })
    @PostMapping("/orgFileRepository/deleteOrgFile")
    public ResponseDto deleteOrgFile(@RequestBody OrgFileRepositoryDTO orgFileRepositoryDTO) {
        if (orgFileRepositoryDTO.getId() == null) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        Message<Boolean> booleanMessage = orgFileRepositoryClient.removeOrgFile(orgFileRepositoryDTO);
        if (booleanMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        return ResultUtil.msg(booleanMessage);
    }

    @ApiOperation(value = "上传资料文件", notes = "上传资料文件")
    @PostMapping("/orgFileRepository/uploadFile")
    public ResponseDto uploadFile(UploadEntity entity) throws IOException {
        List<HbFileInfo> hbFileInfoList = persistenceService.uploadServer(entity);
        ApiUploadEntity uploadEntity = new ApiUploadEntity();
        uploadEntity.setFileInfos(hbFileInfoList);
        return uploadFile(uploadEntity);
    }


    @ApiOperation(value = "上传资料文件", notes = "上传资料文件")
    @PostMapping("/orgFileRepository/uploadFile2")
    public ResponseDto uploadFile(@RequestBody ApiUploadEntity uploadEntity) {
        List<HbFileInfo> hbFileInfoList = uploadEntity.getFileInfos();
        if (CollUtil.isEmpty(hbFileInfoList)) {
            return ResultUtil.FAIL;
        }

        String fileName = uploadEntity.getFileName();

        List<OrgFileRepositoryDTO> listOrgFiles = CollUtil.list(false);
        for (HbFileInfo fileInfo : hbFileInfoList) {
            if (StrUtil.isNotEmpty(fileName)) {
                fileInfo.setRawFileName(fileName);
            }
            OrgFileRepositoryDTO orgFileRepositoryDTO = BeanUtil.copyProperties(fileInfo, OrgFileRepositoryDTO.class);
            orgFileRepositoryDTO.setOrgId(ShiroUtil.getOrgId());
            orgFileRepositoryDTO.buildBaseData(1, ShiroUtil.getUsername(), ShiroUtil.getUserId());
            listOrgFiles.add(orgFileRepositoryDTO);
        }

        Message<Boolean> booleanMessage = orgFileRepositoryClient.saveOrgFile(listOrgFiles);
        if (booleanMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        return ResultUtil.msg(booleanMessage);
    }
}
